var categoriesSelected = new Array();

function addCategoryId ( id, name ){
    if(  categoriesSelected.indexOf( id ) >= 0 ) {
        return false;
    }
    categoriesSelected.push( id );
    var currentLi = $('<li></li>').attr('id','selected_category_' + id ).appendTo('#selected_categories');
    $('<a href="#categories_div"></a>').text( name )

    .click( function() {
        $('#selected_category_' + id ).remove();
        categoriesSelected.splice( categoriesSelected.indexOf(id),1 );
        
    })
    .appendTo( currentLi );
}

function createCategory( name )
{
    $.ajax({
        url: "/categories/add/name/" + name ,
        success: function (data ) {
            alert(data);
        }
    });
}

function fillChildCategory( parentId ) {

    $('#2th_category').empty();
    $('#3th_category').empty();
    $.ajax({
        url: "/categories/getall/parent/" + parentId ,
        dataType: 'json',
        success: function (data ) {
            $.each(data, function(i,v){
                var currentUl = $('<ul></ul>').appendTo('#2th_category');
                var currentLi = $('<li></li>').attr( {
                    'class': 'itemLi'
                } ).appendTo(currentUl);
                if( v.childs == true ) {
                    var name = v.name + ' >>';
                } else {
                    var name = v.name ;
                }
                $('<a href="#categories_div"></a>').text(name)
                .click(
                    function () {
                        if( v.childs == true ) {
                            $.ajax({
                                url: "/categories/getall/parent/" + v.category_id ,
                                dataType: 'json',
                                success: function (data ) {
                                    $('#3th_category').empty();
                                    $.each(data, function(j,c){
                                        var current2Ul = $('<ul></ul>').appendTo('#3th_category');
                                        var current2Li = $('<li></li>').attr( {
                                            'class': 'itemLi'
                                        } ).appendTo(current2Ul);
                                        $('<a href="#categories_div"></a>').text(c.name  ).
                                        click( function(){
                                            addCategoryId( c.category_id, c.name );
                                        })
                                        .appendTo( current2Li );
                                    });
                                }
                            });
                        } else  {
                            addCategoryId( v.category_id, v.name );
                        }
                    }
                )
                .appendTo( currentLi );
            });
        }
    });
    $('#searchCategory').focus();
}

$(document).ready(function() {
    $('#course_form').submit(function(){
        $('#categories').val( categoriesSelected.join(',') );
        return true;
    });
    $("#course_form").validate();
    
    // Autocomplete de categorias
    $("#searchCategory").autocomplete(categoriesChilds, {
        minChars: 0,
        width: 310,
        matchContains: "word",
        autoFill: false,
        formatItem: function(row, i, max) {
            return row.name;
        },
        formatMatch: function(row, i, max) {
            return row.name;
        },
        formatResult: function(row) {
            // addCategoryId(row.category_id, row.name);
            return row.name + ' - '+ row.category_id ;
        }
    });
    $('#addCategoryResults').click(function(){

        var catSelected = $("#searchCategory").val();
        var res = catSelected.split('-');
        addCategoryId( res[1].trim(), res[0].trim() );
    });//
    if( categoriesToAdd   ) {
        $.each( categoriesToAdd, function(j,c){
           addCategoryId( c['category_id'], c['name']) ;
        });
    }
});


